import pandas as pd

def normalize_list(lst):
    amin, amax = min(lst), max(lst)
    for i, val in enumerate(lst):
        lst[i] = (val-amin) / (amax-amin)
    return lst

def main():
    # import variable data for missions
    df_missions = pd.read_csv("variable_data/military_missions.csv")
    # create dataframe for confounding variables
    column_names = ["year", "missions_un", "missions_eu", "missions_nato", "casualties", "election"]
    cv = pd.DataFrame(columns = column_names)
    # calculate scores for missions
    for year in range(1990,2018):
        i = year - 1990
        missions_un = 0
        missions_eu = 0
        missions_nato = 0
        for index, row in df_missions.iterrows():
            if year == row["start"]:
                if row["type"] == "un":
                    missions_un += 1
                elif row["type"] == "nato":
                    missions_nato += 1
                elif row["type"] == "eu":
                    missions_eu += 1     
        cv.loc[i, "year"] = str(year).split(".")[0]
        cv.loc[i, "missions_un"] = missions_un
        cv.loc[i, "missions_eu"] = missions_eu
        cv.loc[i, "missions_nato"] = missions_nato
    # filter for German missions
    df_missions = df_missions[df_missions.german_contribution == 1]
    # import and include data for casualties and elections
    df_casualties = pd.read_csv("variable_data/german_casualties.csv")
    cv["casualties"] = df_casualties.casualties
    df_election = pd.read_csv("variable_data/election_years.csv")
    cv["election"] = df_election.election_year
    # create dummy variables for missions
    cv["missions_un_dummy"] = 0
    cv["missions_eu_dummy"] = 0
    cv["missions_nato_dummy"] = 0
    # turn missions scores into dummy variables
    for index, row in cv.iterrows():
        if index != 0:
            index_temp = index - 1
            if row["missions_un"] > cv.loc[index_temp, "missions_un"]:
                cv.loc[index, "missions_un_dummy"] = 1
            if row["missions_eu"] > cv.loc[index_temp, "missions_eu"]:
                cv.loc[index, "missions_eu_dummy"] = 1
            if row["missions_nato"] > cv.loc[index_temp, "missions_nato"]:
                cv.loc[index, "missions_nato_dummy"] = 1
    # export data
    cv = cv[["year", "missions_un_dummy", "missions_eu_dummy", "missions_nato_dummy", "casualties", "election"]]
    cv.to_csv("cv.csv", index = False)

if __name__ == "__main__":
    main()